﻿using System.Collections.Generic;
using System.Linq;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class BuffEffective_AttrChange : BuffEffective
    {
        protected override void OnInitProperty(BuffEffectData rData, BuffComponent rTarget)
        {
            base.OnInitProperty(rData, rTarget);
            var rConfig = new ConfigData();
            if (rData.param.Count > 1)
            {
                rConfig.attrType = (EAttributeType)rData.param[0];
                    rConfig.value = rData.param[1];
            }
            if (rData.param.Count > 2)
            {                
                rConfig.percent = rData.param[2] % 2 == 1; // 0 add 1 percent 2 formula 3 formula percent
                rConfig.formula = rData.param[2] / 2 == 1;
            }
            rData.userData = rConfig;
        }

        protected override void OnActive(BuffEffectContext rContext, BuffEffectData rConfig, int nRuntimeParam)
        {
            base.OnActive(rContext, rConfig, nRuntimeParam);
            if (rConfig.userData is ConfigData rConfigData)
            {
                var rBuffComp = rContext.buff.target;
                if (rBuffComp == null)
                {
                    return;
                }
                var rAttrComp = rBuffComp.unit.GetComponent<AttributeComponent>(ETComponentType.AttributeComponent);
                if (rAttrComp == null)
                {
                    return;
                }

                var value = rConfigData.formula ? Formula.Calculate<LFloat>(rConfigData.value, rContext.buff.maker.unit, rContext.buff.target.unit).ToInt() : rConfigData.value;
                rContext.result = value;
                rAttrComp.Add(rConfigData.attrType, value, rConfigData.percent ? EAttributeAddType.Percent : EAttributeAddType.Add);
            }
        }

        protected override void OnDeActive(BuffEffectContext rContext, BuffEffectData rConfig)
        {
            base.OnDeActive(rContext, rConfig);
            if (rConfig.userData is ConfigData rConfigData)
            {
                var rTarget = rContext.buff.target;
                var rAttrComp = rTarget.unit?.GetComponent<AttributeComponent>(ETComponentType.AttributeComponent);
                if (rAttrComp != null)
                {
                    var value = (int)rContext.result;
                    rAttrComp.Add(rConfigData.attrType, -value, rConfigData.percent ? EAttributeAddType.Percent : EAttributeAddType.Add);
                }
            }
        }

        private class ConfigData : BuffEffectiveConfig
        {
            public EAttributeType attrType;
            public int value;
            public bool percent;
            public bool formula;
        }

#if UNITY_EDITOR
        public override string ToString(BuffEffectData rConfig)
        {
            var rConfigData = rConfig.userData as ConfigData;
            return $"修改[{rConfigData.attrType}]属性[{rConfigData.value}]";
        }
#endif
    }
}